package top.zhangjianyong.tools.service.api.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.zhangjianyong.tools.entity.RemindItem;
import top.zhangjianyong.tools.factory.RemindItemFactory;
import top.zhangjianyong.tools.parm.RemindItemAddParam;
import top.zhangjianyong.tools.parm.RemindItemUpdateParam;
import top.zhangjianyong.tools.service.api.RemindItemService;
import top.zhangjianyong.tools.service.mybatis.IRemindItemService;
import top.zhangjianyong.tools.utils.ApiResponse;
import top.zhangjianyong.tools.user.UserHolder;

/**
 * todo 实现提醒事项
 *
 * @author ： zhangjianyong
 * @date ： 2021/11/1 下午7:24
 */
@SuppressWarnings("rawtypes")
@Service
public class RemindItemServiceImpl implements RemindItemService {

    @Autowired
    private IRemindItemService iRemindItemService;

    @Override
    public ApiResponse add(RemindItemAddParam remindItemAddParam) {
        RemindItem remindItem = RemindItemFactory.create(remindItemAddParam);
        iRemindItemService.save(remindItem);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse update(RemindItemUpdateParam remindItemUpdateParam) {
        RemindItem remindItem = RemindItemFactory.create(remindItemUpdateParam);
        if (iRemindItemService.updateById(remindItem)) {
            return ApiResponse.success();
        }
        return ApiResponse.fail("更新失败");
    }

    @Override
    public ApiResponse delFlag(String id, int flag) {
        iRemindItemService.lambdaUpdate()
                .eq(RemindItem::getId, id)
                .set(RemindItem::getDelFlag, flag)
                .update();
        return ApiResponse.success();
    }

    @Override
    public ApiResponse del(String id) {
        iRemindItemService.removeById(id);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse page(int page, int size) throws Exception {
        Long userId = UserHolder.userId();

        Page<RemindItem> p = iRemindItemService.lambdaQuery()
                .eq(RemindItem::getCreateBy, userId)
                .orderByDesc(RemindItem::getCreateTime,
                        RemindItem::getId)
                .page(new Page<>(page, size));

        return ApiResponse.success(p);
    }


    @Override
    public ApiResponse close(Long id, String uuid) throws Exception {
        iRemindItemService.close(id, uuid);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse closeNoCheck(Long id) throws Exception {
        iRemindItemService.close(id);
        return ApiResponse.success();
    }

    @Override
    public ApiResponse get(String id) {
        RemindItem remindItem = iRemindItemService.getById(id);
        return ApiResponse.success(remindItem);
    }

    @Override
    public ApiResponse resetAll() {
        iRemindItemService.reset("工作日");
        iRemindItemService.reset("每天");
        return ApiResponse.success();
    }
}
